package com.ccnode.codegenerator.taikaoback.security.handler;

import com.ccnode.codegenerator.taikaoback.util.R;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证失败处理器
 */
@Component
public class AuthEntryPoint implements AuthenticationEntryPoint {
    
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        // 设置响应状态码
        response.setStatus(200);
        // 设置响应头
        response.setContentType("application/json;charset=UTF-8");
        // 设置响应内容
        response.getWriter().write(JSON.toJSONString(R.error(401, "认证失败，请重新登录")));
    }
}